// @algorithm @lc id=100332 lang=cpp
// @title zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof

#include "algm/algm.h"
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if (s.length() == 0) {
            return 0;
        }
        unordered_map<char, int> hash;
        int ans = 0, left = 0, right = 0;
        for (int i = 0; i < (int)s.length(); ++i) {
            if (hash.count(s[i]) == 0) {
                hash.insert({s[i], i});
                right++;
            }
            else {
                int del = hash[s[i]];
                while (left <= del) {
                    hash.erase(s[left]);
                    ++left;
                }
                hash.insert({s[i], i});
                right++;
            }
            ans = max(ans, right - left);
        }
        return ans;
    }
};